package com.niit.mobileDevide.screenon;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class ScreenOnBean implements WritableComparable<ScreenOnBean> {
    private int userId; // 用户id
    private double appUsageTime; // 应用程序使用时间 (min/day)
    private double screenOnTime; // 屏幕开启时间 (hours/day)
    private double batteryDrain; // 电池消耗 (mAh/day)
    private int numAppsInstalled; // 已安装应用数量
    private double dataUsage; // 数据使用量 (MB/day)

    // 无参构造器
    public ScreenOnBean() {}

    // 带参构造器
    public ScreenOnBean(int userId, double appUsageTime, double screenOnTime, double batteryDrain, int numAppsInstalled, double dataUsage) {
        this.userId = userId;
        this.appUsageTime = appUsageTime;
        this.screenOnTime = screenOnTime;
        this.batteryDrain = batteryDrain;
        this.numAppsInstalled = numAppsInstalled;
        this.dataUsage = dataUsage;
    }

    // 用于Mapper中的简化构造器
    public ScreenOnBean(String userId, double screenOnTime) {
        try {
            this.userId = Integer.parseInt(userId.trim());
            this.screenOnTime = screenOnTime;
        } catch (NumberFormatException e) {
            // 处理可能的解析错误
            System.err.println("Error parsing userId: " + userId + ". Error: " + e.getMessage());
            this.userId = -1; // 或者抛出异常，根据业务逻辑决定
            this.screenOnTime = screenOnTime;
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(userId);
        out.writeDouble(appUsageTime);
        out.writeDouble(screenOnTime);
        out.writeDouble(batteryDrain);
        out.writeInt(numAppsInstalled);
        out.writeDouble(dataUsage);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        userId = in.readInt();
        appUsageTime = in.readDouble();
        screenOnTime = in.readDouble();
        batteryDrain = in.readDouble();
        numAppsInstalled = in.readInt();
        dataUsage = in.readDouble();
    }

    @Override
    public String toString() {
        return String.format("%d, %.2f, %.2f, %.2f, %d, %.2f", userId, appUsageTime, screenOnTime, batteryDrain, numAppsInstalled, dataUsage);
    }

    // 实现Comparable接口的compareTo方法
    @Override
    public int compareTo(ScreenOnBean other) {
        return Double.compare(this.screenOnTime, other.screenOnTime); // 按屏幕开启时间升序排序
    }

    // Getters and Setters (省略)

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public double getAppUsageTime() {
        return appUsageTime;
    }

    public void setAppUsageTime(double appUsageTime) {
        this.appUsageTime = appUsageTime;
    }

    public double getScreenOnTime() {
        return screenOnTime;
    }

    public void setScreenOnTime(double screenOnTime) {
        this.screenOnTime = screenOnTime;
    }

    public double getBatteryDrain() {
        return batteryDrain;
    }

    public void setBatteryDrain(double batteryDrain) {
        this.batteryDrain = batteryDrain;
    }

    public int getNumAppsInstalled() {
        return numAppsInstalled;
    }

    public void setNumAppsInstalled(int numAppsInstalled) {
        this.numAppsInstalled = numAppsInstalled;
    }

    public double getDataUsage() {
        return dataUsage;
    }

    public void setDataUsage(double dataUsage) {
        this.dataUsage = dataUsage;
    }
}